﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace dzs.Web.SysMan
{
    public partial class AddUser : Web.Admin.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;

                    int roles_id = Convert.ToInt32(ds.Tables[0].Rows[0]["roles_id"].ToString());
                    int user_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"].ToString());

                    if (roles_id == 1)//管理员
                    {
                        dzs.BLL.roles rm = new BLL.roles();
                        DataSet ds_role = rm.GetList("");

                        this.ddl_roles.DataSource = ds_role;
                        this.ddl_roles.DataTextField = "roles_name";
                        this.ddl_roles.DataValueField = "roles_id";
                        this.ddl_roles.DataBind();

                        if (!string.IsNullOrEmpty(Request["user_id"]))
                        {
                            int user_ids = Convert.ToInt32(Request["user_id"]);
                            ShowInfo(user_ids);
                        }
                        
                    }
                    else
                    {
                        this.btnSave.Visible = false;
                    }
                   

                }
            }
        }

        private void ShowInfo(int user_id)
        {
            dzs.BLL.user bll = new dzs.BLL.user();
            dzs.Model.user model = bll.GetModel(user_id);
           
            this.txtuser_name.Text = model.user_name;
            this.txtuser_login.Text = model.user_login;
            this.txtuser_pass.Text = model.user_pass;
            this.txtuser_mobelPhone.Text = model.user_mobelPhone;
            this.txtuser_phone.Text = model.user_phone;
            this.txtuser_fax.Text = model.user_fax;
            this.txtuser_company.Text = model.user_company;
            this.txtuser_email.Text = model.user_email;
            this.ddl_roles.SelectedValue = model.roles_id.ToString();
            this.txtremark.Text = model.remark;
            //this.txtsid.Text = model.sid.ToString();

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string user_name = this.txtuser_name.Text;
            string user_login = this.txtuser_login.Text;
            string user_pass = this.txtuser_pass.Text;
            string user_mobelPhone = this.txtuser_mobelPhone.Text;
            string user_phone = this.txtuser_phone.Text;
            string user_fax = this.txtuser_fax.Text;
            string user_company = this.txtuser_company.Text;
            string user_email = this.txtuser_email.Text;
            int roles_id = int.Parse(this.ddl_roles.SelectedValue);
            string remark = this.txtremark.Text;
            //int sid = int.Parse(this.txtsid.Text);
            int sid = 0;

            dzs.BLL.user bll = new dzs.BLL.user();

            if (string.IsNullOrEmpty(Request["user_id"]))
            {
                dzs.Model.user model = new dzs.Model.user();
                model.user_name = user_name;
                model.user_login = user_login;
                model.user_pass = user_pass;
                model.user_mobelPhone = user_mobelPhone;
                model.user_phone = user_phone;
                model.user_fax = user_fax;
                model.user_company = user_company;
                model.user_email = user_email;
                model.roles_id = roles_id;
                model.remark = remark;
                model.sid = sid;

                bll.Add(model);
            }
            else
            {
                int user_ids = Convert.ToInt32(Request["user_id"]);

                dzs.Model.user model = new dzs.BLL.user().GetModel(user_ids);
                
                model.user_name = user_name;
                model.user_login = user_login;
                model.user_pass = user_pass;
                model.user_mobelPhone = user_mobelPhone;
                model.user_phone = user_phone;
                model.user_fax = user_fax;
                model.user_company = user_company;
                model.user_email = user_email;
                model.roles_id = roles_id;
                model.remark = remark;
                model.sid = sid;

                bll.Update(model);

            }
            Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "ListUserInfo.aspx");
        }
    }
}